package com.zkt.auth.common.context;

import cn.hutool.core.collection.CollUtil;
import com.zkt.auth.common.constants.AuthConstant;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/23
 * @description 登录上下文对象
 **/

public class LoginContextHolder {
	
	private static final InheritableThreadLocal<Map<String, Object>> THREAD_LOCAL = new InheritableThreadLocal<>();
	
	public static void set(String key, Object value) {
		Map<String, Object> map = getThreadLocalMap();
		map.put(key, value);
	}
	
	public static Object get(String key) {
		Map<String, Object> map = getThreadLocalMap();
		return map.get(key);
	}
	
	public static String getLoginId() {
		return String.valueOf(getThreadLocalMap().get(AuthConstant.LOGIN_ID));
	}
	
	public static void remove() {
		THREAD_LOCAL.remove();
	}
	
	private static Map<String, Object> getThreadLocalMap() {
		Map<String, Object> map = THREAD_LOCAL.get();
		if (CollUtil.isEmpty(map)) {
			map = new ConcurrentHashMap<>();
			THREAD_LOCAL.set(map);
		}
		
		return map;
	}
	
}